<template>
  <el-button @click="handleClick" type="primary">
    <slot></slot>
  </el-button>
  <div class="m-choose-icon-dialog-body-height">
    <el-dialog :title="title" v-model="dialogVisible">
      <div class="container">
        <div
          class="item"
          v-for="(item, index) in Object.keys(ElIcons)"
          :key="index"
          @click="clickItem(item)"
        >
          <el-icon class="text-2xl">
            <component :is="item" />
          </el-icon>
          <div class="icon">{{ item }}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import * as ElIcons from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { toLine } from '@/utils'
import { useCopy } from '@/hooks/useCopy'

const props = defineProps({
  title: String,
  visible: Boolean,
})
const emit = defineEmits(['update:visible'])

const dialogVisible = ref(props.visible)

const handleClick = () => {
  emit('update:visible', !props.visible)
}

const clickItem = (item) => {
  const text = `<el-icon><${item}/></el-icon>`
  useCopy(text)
  dialogVisible.value = false
}

watch(
  () => props.visible,
  (newValue) => {
    dialogVisible.value = newValue
  },
)

watch(dialogVisible, (newValue) => {
  emit('update:visible', newValue)
})
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.item {
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  cursor: pointer;
  height: 70px;
}
.text {
  font-size: 14px;
}
.icon {
  flex: 1;
}
svg {
  width: 2em;
  height: 2em;
}
</style>
